package com.immunology.servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.immunology.database.ConnectionManager;
import com.immunology.model.MappingPages;

public class ShowSyndromeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ShowSyndromeServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		getServletContext()
				.getRequestDispatcher(MappingPages.SHOW_SYNDROME_JSP).forward(
						request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		ConnectionManager connectionManager = new ConnectionManager();
		Connection connection = connectionManager.getConnection();
		String sql = "";
		int id;
		@SuppressWarnings("unchecked")
		Enumeration<String> enumeration = request.getParameterNames();
		while (enumeration.hasMoreElements()) {
			id = Integer.parseInt(enumeration.nextElement());
			sql = "DELETE FROM syndrome WHERE id = " + id + "";
		}
		Statement statement = null;
		try {
			statement = connection.createStatement();
			int set = statement.executeUpdate(sql);
			if (set > 0) {
				request.getRequestDispatcher(MappingPages.SHOW_SYNDROME_JSP)
						.forward(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				connectionManager.closeConnection(connection);
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
